<script setup>
import { PlusOutlined, SearchOutlined, RedoOutlined } from '@ant-design/icons-vue'
import { getOptions } from '@/utils'
import { ref } from 'vue'

const emits = defineEmits(['add', 'init', 'search'])

// 新增按钮点击
const add = () => {
  emits('add')
}
// 刷新
const name = ref('')
const type = ref(null)
const isUp = ref(null)
const init = () => {
  name.value = ''
  type.value = null
  isUp.value = null
  emits('init')
}
// 搜索
const search = () => {
  emits('search', {
    name: name.value ?? null,
    type: type.value ?? null,
    isUp: isUp.value ?? null
  })
}
</script>

<template>
  <div class="head">
    <AButton type="primary" @click="add"> <PlusOutlined /> 新增 </AButton>
    <div class="right">
      <a-input
        @input="search"
        v-model:value="name"
        placeholder="请输入商品名称查询"
        allowClear
        style="width: 200px"
      >
        <template #suffix>
          <SearchOutlined />
        </template>
      </a-input>
      <a-select
        style="width: 200px"
        @change="search"
        placeholder="筛选分类"
        v-model:value="type"
        :options="getOptions('sp_type')"
        allowClear
        :fieldNames="{ label: 'constLabel', value: 'constValue' }"
      />
      <a-select
        style="width: 200px"
        @change="search"
        placeholder="是否上架"
        v-model:value="isUp"
        :options="[
          { label: '已上架', value: 1 },
          { label: '未上架', value: 2 }
        ]"
        allowClear
      />
      <AButton type="primary" @click="search"> <SearchOutlined /> 搜索 </AButton>
      <AButton type="primary" @click="init"> <RedoOutlined /> 刷新 </AButton>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.head {
  display: flex;
  gap: 8px;
  justify-content: space-between;

  .right {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
  }
}
</style>